package com.runchinaup.smartbelt.activity;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.runchinaup.runchilibrary.modes.activity.RootActivity;
import com.runchinaup.smartbelt.R;
import com.runchinaup.smartbelt.ble.BlueGattCallbackHelper;
import com.runchinaup.smartbelt.domian.AlarmCanData;
import com.runchinaup.smartbelt.domian.AlarmData;
import com.runchinaup.smartbelt.domian.BleDevice;
import com.runchinaup.smartbelt.preferences.AlarmCanInfoPre;
import com.runchinaup.smartbelt.preferences.AlarmInfoPre;
import com.runchinaup.smartbelt.view.pop.AlarmPop;
import com.suke.widget.SwitchButton;

import java.util.ArrayList;

/**
 * Created by liber on 2018/1/26.
 */

public class AlarmActivity extends RootActivity implements BlueGattCallbackHelper.OnCallBackListener {

    private ListView listView;
    private AlarmAdapter adapter;

    private BlueGattCallbackHelper gattCallbackHelper = BlueGattCallbackHelper.getBlueGattCallbackHelper();

    //记录闹钟使能，，闹钟开还是关
    AlarmCanData alarmCanData;
    //记录闹钟的信息，，设置的时间
    ArrayList<AlarmData> alarmList;

    @Override
    public int loadLayout() {
        return R.layout.ui_alarm;
    }

    @Override
    public void initView() {
        //标题栏中间文字
        TextView centerText = $View(R.id.centerText);
        centerText.setText(R.string.phone_alarm);
        //标题栏左边图片
        ImageView leftInco = $View(R.id.leftIcon);
        leftInco.setImageResource(R.mipmap.back_white);

        alarmList = AlarmInfoPre.read(this);
        if (alarmList == null) {
            alarmList = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                alarmList.add(new AlarmData());
            }
            gattCallbackHelper.getAlarmTime(0);
        }
        Log.e("debug==", alarmList.size() + "===>");

        alarmCanData = AlarmCanInfoPre.read(this);
        if (alarmCanData == null) {
            alarmCanData = new AlarmCanData();
            gattCallbackHelper.getCanAlarm();
        }
        init();
    }

    private void init() {

        listView = $View(R.id.alarm_list);
        adapter = new AlarmAdapter(this, alarmList) {
            @Override
            protected void onChangeTime(AlarmData alarmData, TextView alarmTime, SwitchButton switchButton, int position) {
                setAlarm(alarmTime, switchButton, position);
//                adapter.notifyDataSetChanged();
            }

            @Override
            protected void onSwitchButton(SwitchButton switchButton, boolean isChecked, int position) {
                switchButtonChange(position, isChecked);
            }

            @Override
            protected void weekDataCallback(int weekData, int position) {
                alarmList.get(position).setAlarmWeek((byte) weekData);
                AlarmInfoPre.save(AlarmActivity.this, alarmList);
                //发送闹钟设置的时间
                gattCallbackHelper.setAlarmData(alarmList.get(position));
            }
        };

        gattCallbackHelper.resigistBlueCallback(this);

        listView.setAdapter(adapter);

    }

    @Override
    public void onLeftClick(View view) {
        finish();
    }


    //设置和保存闹钟的数据
    private void setAlarm(final TextView alarmTime, final SwitchButton switchButton, final int i) {
        String time = alarmTime.getText().toString().trim();
        new AlarmPop(this).showPicker($View(R.id.alarmLayout), time, new AlarmPop.ClickCallback() {
            @Override
            public void onSelect(int hour, int minute) {
                alarmTime.setText(String.format("%02d:%02d", hour, minute));
                switchButton.setChecked(true);
                //每次设置闹钟时间后，自动打开闹钟
                makeCanAlarm(i, true);
                //保存设置闹钟的时和分
                alarmList.get(i).setAlarmNum((byte) i);
                alarmList.get(i).setAlarmHour((byte) hour);
                alarmList.get(i).setAlarmMintue((byte) minute);
                AlarmInfoPre.save(AlarmActivity.this, alarmList);
                //发送闹钟设置的时间
                gattCallbackHelper.setAlarmData(alarmList.get(i));
            }
        });
    }

    //保存闹钟的状态和发送闹钟设置的时间
    private void switchButtonChange(int i, boolean isChecked) {
        makeCanAlarm(i, isChecked);
        if (isChecked) {
            gattCallbackHelper.setAlarmData(alarmList.get(i));
        }
    }

    //设置和保存闹钟使能情况
    private void makeCanAlarm(int i, boolean openOrClose) {
        switch (i) {
            case 0:
                alarmCanData.setOneAlarm(openOrClose);
                break;
            case 1:
                alarmCanData.setTwoAlarm(openOrClose);
                break;
            case 2:
                alarmCanData.setThreeAlarm(openOrClose);
                break;
        }
        AlarmCanInfoPre.save(this, alarmCanData);
        //发送闹钟使能
        gattCallbackHelper.setCanAlarm(alarmCanData);
    }

    @Override
    public void linkCall(String bluetoothPosition, BleDevice bleDevice) {

    }

    @Override
    public void onState(int state, String bluetoothPosition, BleDevice bleDevice) {

    }

    @Override
    public void getElectricity(byte[] data) {

    }

    @Override
    public void onExceptionDis(String bluetoothPosition, BleDevice device) {

    }

    //获取闹钟使能
    @Override
    public void getAlarmCan(byte[] data) {

        //第一个闹钟是开还是关
        if ((data[5] & 0xff) == 0xff) {
            alarmCanData.setOneAlarm(true);
        }
        //第二个闹钟是开还是关
        if ((data[6] & 0xff) == 0xff) {
            alarmCanData.setTwoAlarm(true);
        }
        //第三个闹钟是开还是关
        if ((data[7] & 0xff) == 0xff) {
            alarmCanData.setThreeAlarm(true);
        }

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AlarmCanInfoPre.save(AlarmActivity.this, alarmCanData);
                adapter.notifyDataSetChanged();
            }
        });
    }

    //获取闹钟时间
    @Override
    public void getAlarmTime(byte[] data) {

        byte b = (byte) (data[4] & 0xff);
        switch (b) {
            case 0x00:
                makeAlarmTime(0, data);
                break;
            case 0x01:
                makeAlarmTime(1, data);
                gattCallbackHelper.getAlarmTime(1);
                break;
            case 0x02:
                makeAlarmTime(2, data);
                gattCallbackHelper.getAlarmTime(2);
                break;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AlarmInfoPre.save(AlarmActivity.this, alarmList);
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void makeAlarmTime(int i, byte[] data) {
        alarmList.get(i).setAlarmHour((byte) (data[5] & 0xff));
        alarmList.get(i).setAlarmMintue((byte) (data[6] & 0xff));
        alarmList.get(i).setAlarmWeek((byte) (data[8] & 0xff));
    }
}